import { Schema, Document } from 'mongoose';
import * as mongoose from 'mongoose';

export interface ICarEntity extends Document {

    /** CarEntity id */
    id?: (string | null);

    /** CarEntity car */
    car?: (ICar | null);
}

interface ILocation extends Document {
    /** Location longitude */
    longitude?: (number | null);

    /** Location latitude */
    latitude?: (number | null);
}

export enum CarStatus {
    CS_NOT_SPECIFIED = 0,
    LOCKED = 1,
    UNLOCKING = 2,
    UNLOCKED = 3,
    LOCKING = 4
}

interface IDriver extends Document {

    /** Driver id */
    id?: (string | null);

    /** Driver avatarUrl */
    avatarUrl?: (string | null);
}

interface ICar extends Document {

    /** Car status */
    status?: (CarStatus | null);

    /** Car driver */
    driver?: (IDriver | null);

    /** Car position */
    position?: (ILocation | null);

    /** Car tripId */
    tripId?: (string | null);
}

const CarEntitySchema = new Schema({
    id: { type: Schema.Types.ObjectId },
    car: {
        status: Number,
        driver: {
            id: String,
            avatarurl: String
        },
        position: {
            longitude: Number,
            latitude: Number
        },
        tripid: String
    }
});

// mongoose.model('Car', CarEntitySchema, 'car')碰到的bug：https://www.jianshu.com/p/c2a2d0849eba
export const Car = mongoose.model<ICarEntity>('Car', CarEntitySchema, 'car')
